package com.promis.pfs;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import com.promis.fastdfs.FastFile;
import com.promis.fastdfs.FastFileBytes;
import com.promis.fastdfs.FastFilePath;
import com.promis.fastdfs.FastFileStorageClient;
import com.promis.fastdfs.FastStore;
import com.promis.fastdfs.StorageClient;
import com.promis.fastdfs.StorageClientFactory;
import com.promis.okhttp.HttpFileParams;
import com.promis.okhttp.HttpStringParams;
import com.promis.okhttp.HttpUploadClient;
import com.promis.okhttp.JsonNormal;
import com.promis.util.CollectionUtils;
import com.promis.util.FileUtils;
import com.promis.util.StringUtils;

import java.io.File;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.logging.Logger;

public abstract class PfsFileStorager {

    private static String UPLOAD_URL;
    private static String DELETE_URL;

    public static void initUploadUrl(String uploadUrl) {
        UPLOAD_URL = uploadUrl;
    }

    public static void initDeleteUrl(String deleteUrl) {
        DELETE_URL = deleteUrl;
    }

    public static String[] upload(final Context context, PfsFile... pfsFiles) {
        PfsFileStorager.UploadTask myAsyncTask = new PfsFileStorager.UploadTask();
        try {
            UploadParameter params = new UploadParameter(context, pfsFiles);
            return myAsyncTask.execute(params).get(1, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static boolean delete(Context context, String... urlPaths) {
        PfsFileStorager.DeleteTask myAsyncTask = new PfsFileStorager.DeleteTask();
        try {
            DeleteParameter parameter = new DeleteParameter(context, urlPaths);
            return myAsyncTask.execute(parameter).get(1, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
        return false;
    }

    private static class UploadTask extends AsyncTask<UploadParameter, Integer, String[]> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String[] doInBackground(UploadParameter... params) {
            UploadParameter param = params[0];

            PfsFile[] pfsFiles = param.getPfsFiles();
            Context context = param.getContext();

            if (!CollectionUtils.isNullOrEmpty(pfsFiles)) {
                PfsJsonFiles pfsJsonFiles = null;
                try {
                    HttpFileParams httpFileParams = new HttpFileParams();
                    for (PfsFile pfsFile : pfsFiles) {
                        httpFileParams.add("files", pfsFile.getFileName(), pfsFile.getFileByte());
                    }

                    pfsJsonFiles = new PfsFileUploadClient<PfsJsonFiles>(context){
                        @Override
                        public void onProgress(long bytesWritten, long contentLength, boolean done) {
                            super.onProgress(bytesWritten, contentLength, done);
//                            publishProgress();
                        }
                    }.post(UPLOAD_URL, httpFileParams);
                    return pfsJsonFiles.getPaths();
                } catch (Exception ex) {
                    Log.e("文件上传：", ex.getMessage());
                    if (pfsJsonFiles != null) {
                        delete(context, pfsJsonFiles.getPaths());
                    }
                }
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
        }

        @Override
        protected void onPostExecute(String[] remotePaths) {
            super.onPostExecute(remotePaths);
            //当任务执行完成是调用,在UI线程
//            listener.onSuccess(fastStores);
        }
    }

    private static class DeleteTask extends AsyncTask<DeleteParameter, Integer, Boolean> {

        @Override
        protected Boolean doInBackground(DeleteParameter... parameters) {
            DeleteParameter parameter = parameters[0];

            String[] urlPaths = parameter.getUrlPaths();
            if (CollectionUtils.isNullOrEmpty(urlPaths)) return true;

            Context context = parameter.getContext();

                try {
                    HttpStringParams httpStringParams = new HttpStringParams();
                    for (String urlPath : urlPaths) {
                        httpStringParams.add("paths", urlPath);
                    }

                    if (!CollectionUtils.isNullOrEmpty(parameter.getUrlPaths())) {
                        new PfsFileDeleteClient<JsonNormal>(context).post(DELETE_URL, httpStringParams);
                    }
                    return true;
                } catch (Exception ex) {
                    Log.e("网络文件删除：", ex.getMessage());
                }
            return false;
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);
        }
    }

    private static class UploadParameter {
        private Context context;
        private PfsFile[] pfsFiles;

        public UploadParameter(Context context, PfsFile[] pfsFiles) {
            this.context = context;
            this.pfsFiles = pfsFiles;
        }

        public Context getContext() {
            return context;
        }

        public void setContext(Context context) {
            this.context = context;
        }

        public PfsFile[] getPfsFiles() {
            return pfsFiles;
        }

        public void setPfsFiles(PfsFile[] pfsFiles) {
            this.pfsFiles = pfsFiles;
        }
    }

    private static class DeleteParameter {
        private Context context;
        private String[] urlPaths;

        public DeleteParameter(Context context, String[] urlPaths) {
            this.context = context;
            this.urlPaths = urlPaths;
        }

        public Context getContext() {
            return context;
        }

        public void setContext(Context context) {
            this.context = context;
        }

        public String[] getUrlPaths() {
            return urlPaths;
        }

        public void setUrlPaths(String[] urlPaths) {
            this.urlPaths = urlPaths;
        }
    }
}
